package org.example.admin.user;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class updateUserInfoView extends JFrame {
    private JTextField idField;
    private JTextField usernameField;
    private JTextField telField;
    private JPasswordField passwordField;

    private JTextField emailField;
    private JButton updateButton;

    public updateUserInfoView() {
        setTitle("管理端用户信息修改页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(7, 2));

        JLabel idLabel = new JLabel("用户ID");
        idField = new JTextField();

        JLabel usernameLabel = new JLabel("用户名");
        usernameField = new JTextField();

        JLabel passwordLabel = new JLabel("用户密码");
        passwordField = new JPasswordField();

        JLabel telLabel = new JLabel("电话");
        telField = new JTextField();

        JLabel emailLabel = new JLabel("邮箱");
        emailField = new JTextField();
        updateButton = new JButton("更新");

        // 添加组件到窗口
        add(idLabel);
        add(idField);

        add(usernameLabel);
        add(usernameField);

        add(passwordLabel);
        add(passwordField);

        add(telLabel);
        add(telField);

        add(emailLabel);
        add(emailField);

        add(new JLabel()); // 占位
        add(updateButton);

        // 添加更新按钮的点击事件监听器
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateUserInfo();
            }
        });
    }

    private void updateUserInfo() {
        int userId = Integer.parseInt(idField.getText());
        String username=usernameField.getText();
        String password=passwordField.getText();
        String tel = telField.getText();
        String email = emailField.getText();

        try {
            // 连接数据库
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");

            // 创建更新语句
            String updateQuery = "UPDATE air_user SET username =?,password=?,telephone = ?, email = ? WHERE user_id = ?";
            PreparedStatement statement = conn.prepareStatement(updateQuery);

            statement.setString(1, username);
            statement.setString(2, password);
            statement.setString(3, tel);
            statement.setString(4, email);
            statement.setInt(5, userId);

            // 执行更新操作
            int rowsUpdated = statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            conn.close();

            if (rowsUpdated > 0) {
                JOptionPane.showMessageDialog(this, "更新成功");
            } else {
                JOptionPane.showMessageDialog(this, "用户不存在或更新失败");
            }

            // 清空输入框
            clearFields();

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "更新失败：" + e.getMessage());
        }
    }

    private void clearFields() {
        idField.setText("");
        telField.setText("");
        emailField.setText("");
    }

    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            updateUserInfoView updateUserInfoForm = new updateUserInfoView();
//            updateUserInfoForm.setSize(300, 150);
//            updateUserInfoForm.setVisible(true);
//        });
    }
}